package com.tju.ocean_ecology_website.service.impl;

import com.tju.ocean_ecology_website.entity.DonationRecord;
import com.tju.ocean_ecology_website.dao.DonationRecordDao;
import com.tju.ocean_ecology_website.service.DonationRecordService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 捐赠记录表(DonationRecord)表服务实现类
 *
 * @author shaolong
 * @since 2025-04-29 19:35:59
 */
@Service("donationRecordService")
public class DonationRecordServiceImpl implements DonationRecordService {
    @Resource
    private DonationRecordDao donationRecordDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public DonationRecord queryById(Long id) {
        return this.donationRecordDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param donationRecord 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<DonationRecord> queryByPage(DonationRecord donationRecord, PageRequest pageRequest) {
        long total = this.donationRecordDao.count(donationRecord);
        return new PageImpl<>(this.donationRecordDao.queryAllByLimit(donationRecord, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param donationRecord 实例对象
     * @return 实例对象
     */
    @Override
    public DonationRecord insert(DonationRecord donationRecord) {
        this.donationRecordDao.insert(donationRecord);
        return donationRecord;
    }

    /**
     * 修改数据
     *
     * @param donationRecord 实例对象
     * @return 实例对象
     */
    @Override
    public DonationRecord update(DonationRecord donationRecord) {
        this.donationRecordDao.update(donationRecord);
        return this.queryById(donationRecord.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.donationRecordDao.deleteById(id) > 0;
    }
}
